﻿// 订单审核

// 解析URL
var parameter = window.location.href.split("?")[1];
var parameterList = parameter.split("&");
var order_id = decodeURIComponent(parameterList[0]);  //订单编号
var type = decodeURIComponent(parameterList[1]);  //跳转类型（1：上个页面为到店自取；2：上个页面为在线配送）
var orderState = decodeURIComponent(parameterList[2]);  //订单状态
var username = decodeURIComponent(parameterList[3]);  //用户名
var hospital = decodeURIComponent(parameterList[4]);  //医院
var source = decodeURIComponent(parameterList[5]);  //来源
var department = decodeURIComponent(parameterList[6]);  //科室
var state = decodeURIComponent(parameterList[7]);  //状态
var searchword = decodeURIComponent(parameterList[8]);  //查询词
var timefrom = decodeURIComponent(parameterList[9]);  //开始时间
var timeto = decodeURIComponent(parameterList[10]);  //结束时间
var pageNo  = decodeURIComponent(parameterList[11]);  //页码
var pageSize = decodeURIComponent(parameterList[12]);  //每页显示数据数目
var express_price;
var flag;
console.log(parameterList);

if(type == 1){
    $("#label_name").html("自取订单详情");
}
if(type == 2){
    $("#label_name").html("在线订单详情");
    $("#logistics_name").css("display","block");
    $("#price div:nth-child(2)").css("display","block");
    //通过订单编号查询信息
    $.ajax({
        type: "post",
        url: "/ysfwpt/selectOrderDetailByOrderId",
        async: false,
        data: {
            order_id: order_id
        },
        success: function (data) {
            // console.log("success");
            // console.log(data);
            $("#order_id2").text(data.order.order_id);
            $("#receive_person_name").text(data.order.receive_person_name);
            $("#receive_person_phone").text(data.order.receive_person_phone);
            $("#express_id").text(data.order.express_id);
            $("#express_company_name").text(data.order.express_company_name);
            $("#receive_person_addr").text(data.order.receive_person_addr);
            //根据快递单号和快递公司查询物流信息
            $.ajax({
                type: "post",
                url: "/ysfwpt/logisticsInformation",
                async: false,
                data: {
                    name: data.order.express_company_name,
                    id: data.order.express_id
                },
                success: function (data) {
                    // console.log("success");
                    // console.log(data);
                    //当快递公司、快递单号输入错误时（缺少快递公司、快递公司输入错误、缺少快递单号）
                    if(data.result.Success == false){
                        var strhtml = '<tr>';
                        strhtml = strhtml + '<td colspan="3">' + data.result.Reason + '</td>';
                        $("#logisticsdetail_list").append(strhtml);
                    }
                    //当快递公司、快递单号输入正确时
                    if(data.result.Success == true){
                        var logisticsList = data.result.Traces;
                        //查询结果长度为0：没有物流信息
                        if(logisticsList.length == 0){
                            var strhtml = '<tr>';
                            strhtml = strhtml + '<td colspan="3">' + data.result.Reason + '</td>';
                            $("#logisticsdetail_list").append(strhtml);
                        }
                        //查询结果长度不为0：输出物流信息
                        else{
                            for(var i=logisticsList.length-1;i>=0;i--) {
                                var logisticsinfo = logisticsList[i];
                                var strhtml = '<tr>';
                                strhtml = strhtml + '<td>' + logisticsinfo.AcceptTime + '</td>';
                                if(i == logisticsList.length-1){
                                    strhtml = strhtml + '<td><div class="pic_link1 pic_link"></div></td>';
                                }
                                else if(i == 0){
                                    strhtml = strhtml + '<td><div class="pic_link3 pic_link"></div></td>';
                                }
                                else{
                                    strhtml = strhtml + '<td><div class="pic_link2 pic_link"></div></td>';
                                }
                                strhtml = strhtml + '<td>' + logisticsinfo.AcceptStation + '</td></tr>';
                                if(i > 0){
                                    strhtml = strhtml + '<tr><td></td>';
                                    strhtml = strhtml + '<td><div class="pic_link4 pic_link"></div></td>';
                                    strhtml = strhtml + '<td></td></tr>';
                                }
                                $("#logisticsdetail_list").append(strhtml);
                            }
                        }
                    }
                },
                error: function (data) {
                    // console.log("error");
                    // console.log(data);
                }
            });
        },
        error: function (data) {
            // console.log("error");
            // console.log(data);
        }
    });
}

//通过订单编号查询订单信息
$.ajax({
    type: "post",
    url: "/ysfwpt/selectOrderDetailByOrderId",
    async :false,
    data: {
        order_id: order_id
    },
    success: function (data) {
        // console.log("success");
        // console.log(data);
        $("#order_id").text(data.order.order_id);
        $("#patient_name").text(data.order.patient_name);
        $("#treatment_card").text(data.order.treatment_card);
        $("#patient_phone").text(data.order.patient_phone);
        $("#hospital").text(data.order.hospital);
        //待退药订单
        if(type == 1){
            if(orderState == 5){
                $("#process_img").attr("src","/pic/pic_process4.png");
                $("#order_reason").css("display","");
                $("#reason_name").html("退药原因");
                $("#reason").html(data.order.return_drug_reason);
            }
        }
        if(type == 2){
            express_price = data.order.express_price;
            $("#delivery_price").text(data.order.express_price + "元");
            if(orderState == 6){
                $("#process_img").attr("src","/pic/pic_process4.png");
                $("#order_reason").css("display","");
                $("#reason_name").html("退药原因");
                $("#reason").html(data.order.return_drug_reason);
            }
        }
        //判断是否为处方单
        if(data.order.prescription_id == null || data.order.prescription_id == ""){
            flag = 0;
        }
        else{
            flag = 1;
        }
    },
    error: function (data) {
        // console.log("error");
        // console.log(data);
    }
});

//通过订单编号查询药品信息
$.ajax({
    type: "post",
    url: "/ysfwpt/selectMedicineByOrderId",
    data: {
        order_id: order_id
    },
    success: function (data) {
        // console.log("success");
        // console.log(data);
        var medicineList = data.list;
        for(var i=0;i<data.list.length;i++) {
            var no = parseInt(i)+1;
            var medicineinfo = medicineList[i];
            var strhtml = '<tr id="' + i + '" arrId="' + i + '">';
            strhtml = strhtml + '<td>' + no + '</td>';
            strhtml = strhtml + '<td>' + medicineinfo.order_drug_name + '</td>';
            strhtml = strhtml + '<td>' + medicineinfo.order_drug_capacity + '</td>';
            strhtml = strhtml + '<td>' + medicineinfo.order_drug_manufacturer + '</td>';
            strhtml = strhtml + '<td>' + medicineinfo.order_drug_price + '</td>';
            strhtml = strhtml + '<td>' + medicineinfo.order_drug_amount + '</td>';
            strhtml = strhtml + '<td>' + medicineinfo.order_drug_unit + '</td>';
            strhtml = strhtml + '<td>' + medicineinfo.order_total_price + '</td>';
            if(i==0){
                if(flag == 1){
                    strhtml = strhtml + '<td rowspan=' + data.list.length + ' style="color:#5AA0DC">查看</td>';
                }
                else{
                    strhtml = strhtml + '<td rowspan=' + data.list.length + ' style="color:#5AA0DC">无</td>';
                }
                strhtml = strhtml + '<td rowspan=' + data.list.length + ' style="color:#F6B751">' + data.total + '</td>';
            }
            $("#medicinedetail_list").append(strhtml);
        }
        $("#drue_price").text(data.total + "元");
        //type=1,到店自取订单详情，没有配送费，总价等于药价
        if(type == 1){
            $("#total_price").text(data.total + "元");
        }
        //type=1,在线配送订单详情，有配送费，总价等于药价+配送费
        if(type == 2){
            $("#total_price").text(data.total + express_price + "元");
        }
    },
    error: function (data) {
        // console.log("error");
        // console.log(data);
    }
});

//拒绝退款
$("#refuse").click(function () {
    $('#refuse_popup').show();
    $(".shandow").show().css('z-index', '11');
    $('body').css({
        "overflow-x":"hidden",
        "overflow-y":"hidden"
    });
});
//关闭拒绝退款弹窗
$("#refuse_popup_top div,#refuse_popup_bottom button:nth-child(2)").bind("click", function() {
    $("#refuse_popup").hide();
    $(".shandow").hide().css('z-index','-1');
    $('body').css({
        "overflow-x":"auto",
        "overflow-y":"auto"
    });
});
//确定提交拒绝申请
$("#refuse_popup_bottom button:nth-child(1)").bind("click", function() {
    var refuse_reason = $("#refuse_resaon").val();
    if(refuse_reason.length == 0){
        alert("拒绝原因不得为空！");
    }
    else{
        if(type == 1){
            $.ajax({
                type: "post",
                url: "/ysfwpt/updateOrderState",
                data: {
                    order_id: order_id,
                    state: "4",
                    reason_name: "refuse_return_drug_reason",
                    reason: refuse_reason
                },
                success: function (data) {
                    // console.log("success");
                    // console.log(data);
                    back();
                },
                error: function (data) {
                    // console.log("error");
                    // console.log(data);
                }
            });
        }
        if(type == 2){
            $.ajax({
                type: "post",
                url: "/ysfwpt/updateOrderState",
                data: {
                    order_id: order_id,
                    state: "5",
                    reason_name: "refuse_return_drug_reason",
                    reason: refuse_reason
                },
                success: function (data) {
                    // console.log("success");
                    // console.log(data);
                    back();
                },
                error: function (data) {
                    // console.log("error");
                    // console.log(data);
                }
            });
        }
    }
});

//同意退款
$("#agree").click(function () {
    $('#agree_popup').show();
    $(".shandow").show().css('z-index', '11');
    $('body').css({
        "overflow-x":"hidden",
        "overflow-y":"hidden"
    });
});
//关闭同意退款弹窗
$("#agree_popup_top div,#agree_popup_bottom button:nth-child(2)").bind("click", function() {
    $("#agree_popup").hide();
    $(".shandow").hide().css('z-index','-1');
    $('body').css({
        "overflow-x":"auto",
        "overflow-y":"auto"
    });
});
//确定提交同意申请
$("#agree_popup_bottom button:nth-child(1)").bind("click", function() {
    if(type == 1){
        $.ajax({
            type: "post",
            url: "/ysfwpt/updateOrderState",
            data: {
                order_id: order_id,
                state: "6",
                reason_name: "",
                reason: ""
            },
            success: function (data) {
                // console.log("success");
                // console.log(data);
                back();
            },
            error: function (data) {
                // console.log("error");
                // console.log(data);
            }
        });
    }
    if(type == 2){
        $.ajax({
            type: "post",
            url: "/ysfwpt/updateOrderState",
            data: {
                order_id: order_id,
                state: "7",
                reason_name: "",
                reason: ""
            },
            success: function (data) {
                // console.log("success");
                // console.log(data);
                back();
            },
            error: function (data) {
                // console.log("error");
                // console.log(data);
            }
        });
    }
});

//订单信息
$("#orderdetail_name").click(function () {
    $("#orderdetail").css("display","block");
    $("#orderdetail_name").addClass("chose1");
    $("#logistics").css("display","none");
    $("#logistics_name").removeClass("chose2");
});

//物流信息
$("#logistics_name").click(function () {
    $("#orderdetail").css("display","none");
    $("#orderdetail_name").removeClass("chose1");
    $("#logistics").css("display","block");
    $("#logistics_name").addClass("chose2");
});


//返回上个页面
$("#label div:nth-child(2)").click(function () {
    back();
});

var back = function () {
    var name;
    if(type == 1){
        name = "ys_service_offline";
    }
    if(type == 2){
        name = "ys_service_online";
    }
    window.parent.document.getElementById("iframe_right").src = "/ysfwpt/" + name + "?" + username + "&" + hospital + "&" + source + "&" + department + "&" + state + "&" + searchword + "&" + timefrom + "&" + timeto + "&" + pageNo + "&" + pageSize;
};